from django.http import JsonResponse, HttpResponse
from django.views.decorators.csrf import csrf_exempt
import json

@csrf_exempt  # 注意：仅用于示例，生产环境中应正确处理CSRF保护
def login_view(request):
    if request.method == 'POST':
        try:
            data = json.loads(request.body)
            username = data.get('username')
            password = data.get('password')
            
            print(f"Received login request: username={username}, password={password}")
            # 这里应该有验证用户名和密码的逻辑
            # 假设验证通过
            response_data = {'status': 'success', 'message': 'Logged in successfully'}
            return JsonResponse(response_data)
        except json.JSONDecodeError:
            return HttpResponse(status=400, content="Invalid JSON")
    else:
        return HttpResponse(status=405, content="Method Not Allowed")